package com.stars.easyms.logging.monitor;

import com.alibaba.fastjson.JSON;
import com.plumelog.log4j2.appender.KafkaAppender;
import com.plumelog.log4j2.appender.RedisAppender;
import com.stars.easyms.logging.appender.EasyMsPlumeLogAppender;
import com.stars.easyms.logging.constant.EasyMsLoggingConstants;
import com.stars.easyms.logging.properties.EasyMsLoggingLogColorHelper;
import com.stars.easyms.logging.properties.EasyMsLoggingPlumeLogHelper;
import com.stars.easyms.monitor.MonitorRequestHandler;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * <p>className: EasyMsLoggingMonitorRequestHandler</p>
 * <p>description: EasyMs日志模块的监控方法类</p>
 *
 * @author guoguifang
 * @date 2019-11-20 10:25
 * @since 1.4.0
 */
public final class EasyMsLoggingMonitorRequestHandler implements MonitorRequestHandler {

    @Override
    public Map<String, Object> getMonitorInfo() {
        Map<String, Object> returnMap = new LinkedHashMap<>();
        returnMap.put("logColor", getLogColorMap());
        returnMap.put("plumeLog", getPlumeLogMap());
        return returnMap;
    }

    private Map<String, Object> getLogColorMap() {
        Map<String, Object> logColorMap = new LinkedHashMap<>();
        if (!EasyMsLoggingLogColorHelper.isEnabled()) {
            logColorMap.put("enabled", false);
            return logColorMap;
        }
        logColorMap.put("enabled", true);
        logColorMap.put("levelColor", EasyMsLoggingLogColorHelper.getLevelColorMap());
        logColorMap.put("levelAnsiColor", EasyMsLoggingLogColorHelper.getLevelAnsiColorMap());
        return logColorMap;
    }

    private Map<String, Object> getPlumeLogMap() {
        Map<String, Object> plumeLogMap = new LinkedHashMap<>();
        if (!EasyMsLoggingPlumeLogHelper.isEnabled()) {
            plumeLogMap.put("enabled", false);
            return plumeLogMap;
        }
        plumeLogMap.put("enabled", true);
        plumeLogMap.put("appName", EasyMsLoggingPlumeLogHelper.getAppName());
        plumeLogMap.put("send count", EasyMsPlumeLogAppender.getCount());

        boolean isRedisEnabled = EasyMsLoggingPlumeLogHelper.isRedisAppenderEnabled();
        Map<String, Object> redisMap = new LinkedHashMap<>();
        if (isRedisEnabled) {
            RedisAppender redisAppender = EasyMsLoggingPlumeLogHelper.getRedisAppender();
            Map<String, Object> redisAppenderMap = JSON.parseObject(JSON.toJSONString(redisAppender));
            redisMap.put("enabled", true);
            redisMap.put("host", redisAppenderMap.get("redisHost"));
            redisMap.put("port", redisAppenderMap.get("redisPort"));
            redisMap.put("db", redisAppenderMap.get("redisDb"));
            redisMap.put("run model", "1".equals(redisAppenderMap.get("runModel")) ? "high performance" : "all information");
        } else {
            redisMap.put("enabled", false);
        }
        plumeLogMap.put("redis", redisMap);

        boolean isKafkaEnabled = EasyMsLoggingPlumeLogHelper.isKafkaAppenderEnabled();
        Map<String, Object> kafkaMap = new LinkedHashMap<>();
        if (isKafkaEnabled) {
            KafkaAppender kafkaAppender = EasyMsLoggingPlumeLogHelper.getKafkaAppender();
            Map<String, Object> kafkaAppenderMap = JSON.parseObject(JSON.toJSONString(kafkaAppender));
            kafkaMap.put("enabled", true);
            kafkaMap.put("hosts", kafkaAppenderMap.get("kafkaHosts"));
            kafkaMap.put("run model", "1".equals(kafkaAppenderMap.get("runModel")) ? "high performance" : "all information");
        } else {
            kafkaMap.put("enabled", false);
        }
        plumeLogMap.put("kafka", kafkaMap);
        return plumeLogMap;
    }

    @Override
    public String getModuleName() {
        return EasyMsLoggingConstants.MODULE_NAME;
    }

    @Override
    public int getOrder() {
        return 20;
    }

}